home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1998 July / EnigmA AMIGA RUN 29 (1998)(G.R. Edizioni)(IT)[!][issue 1998-07 & 08].iso / earkit / news / thor / rexx / cfgautoreply.thor < prev    next >
Text File  |  1998-05-24  |  15KB  |  458 lines

  1. /*
  2. $VER: CfgAutoReply.thor 1.1 (28.08.96)
  3. (c)  Neil Bothwick <neil@wirenet.co.uk> 1996
  4. */
  5.  
  6. /* Config editor for AutoReply.br, an arexx script for     */
  7. /* Thor to generate an event in reply to an email.         */
  8.  
  9. options results
  10.  
  11. /* ;;;Make sure CfgAutoReply is being run from Thor */
  12. thorport = address()
  13. if left(thorport,5) ~= 'THOR.' then do
  14.     say 'CfgAutoReply.thor must be run from Thor'
  15.     exit 10
  16.     end
  17. ;;;
  18. /* ;;;Ensure rexxsupport and bbsread libraries are loaded */
  19. if ~show('L','rexxsupport.library') then call addlib('rexxsupport.library',0,-30)
  20. if ~show('p', 'BBSREAD') then do
  21.     address command
  22.     'run >nil: `GetEnv THOR/THORPath`bin/LoadBBSRead'
  23.     'WaitForPort BBSREAD'
  24.     end
  25. ;;;
  26. /* ;;;Set parameters */
  27. Changed      = 0
  28. CfgFile      = 'ENV:THOR/AutoReply.cfg'
  29. FieldsMenu   = '"Name" "System" "Conference" "Reply Text" "Subject" "Quote Message" "Quote String" "Signature File" "Header File" "Footer File"'
  30. FieldNames   = '"CONFIG" "SYSTEM" "CONFERENCE" "TEXTFILE" "SUBJECT" "QUOTEMSG" "QUOTESTR" "SIGFILE" "HEADFILE" "FOOTFILE"'
  31. MainMenu     = '"Add config" "Edit config" "Delete config" " " "Use" "Save" " " "Help"'
  32. MainTemplate = 'LINE/M'
  33. MenuTemplate = 'MENU/M'
  34. NameTemplate = 'NAME/M'
  35. address(bbsread)
  36. drop Menu. Fields.
  37. READARGS MainTemplate Menu cmdline MainMenu
  38. if RC > 0 then call ExitMsg(BBSREAD.LASTERROR)
  39. READARGS MenuTemplate Fields cmdline FieldsMenu
  40. if RC > 0 then call ExitMsg(BBSREAD.LASTERROR)
  41. READARGS NameTemplate Fields cmdline FieldNames
  42. if RC > 0 then call ExitMsg(BBSREAD.LASTERROR)
  43. ;;;
  44. /* ;;;Read config file */
  45. address(thorport)
  46. CfgFileSize = word(statef(CfgFile),2)
  47. BytesRead = 0
  48. drop Config.
  49. CfgNo = 0
  50. if ~open(infile,CfgFile,'r') then do
  51.     REQUESTNOTIFY '"Failed to open an existing configuration file"' '" OK "'
  52.     Config.Count = 0
  53.     end
  54. else do
  55.     /* Open progress window */
  56.     OPENPROGRESS title '"CfgAutoReply.thor"' PT '"Reading configuration"'
  57.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  58.     ProgWin = result
  59.     address(bbsread)
  60.  
  61.     do until eof(infile)
  62.         nextln = readln(infile)
  63.         BytesRead = BytesRead + length(nextln) + 1
  64.         if upper(word(nextln,1)) = 'CONFIG' then do
  65.             CfgNo = CfgNo + 1
  66.             drop Args.
  67.             template = 'NAME/A,VALUE/A'
  68.             READARGS template Args CMDLINE nextln
  69.             if RC > 0 then call ExitMsg('Incorrect line in config:\n'||nextline)
  70.             Config.CfgNo = Args.Value
  71.             do until Args.Name = 'ENDCONFIG'
  72.                 if eof(infile) then call ExitMsg('End of config reached without ENDCONFIG')
  73.                 nextln = readln(infile)
  74.                 BytesRead = BytesRead + length(nextln) + 1
  75.                 drop Args. Config.CfgNo.
  76.                 template = 'NAME/A,VALUE'
  77.                 READARGS template Args CMDLINE nextln
  78.                 if RC > 0 then call ExitMsg('Incorrect line in config:\n'||nextline)
  79.                 if Args.Name = 'CONFIG'     then Config.CfgNo = Args.Value
  80.                 else do i = 2 to Fields.Name.Count
  81.                     if Args.Name = Fields.Name.i then do
  82.                         interpret 'Config.CfgNo.'fields.Name.i' = Args.Value'
  83.                         iterate
  84.                         end
  85.                     end
  86.                 end
  87.  
  88.             /* Update progress bar */
  89.             address(thorport)
  90.             UPDATEPROGRESS req ProgWin current BytesRead*100%CfgFileSize
  91.             address(bbsread)
  92.             end
  93.         end
  94.     call close(infile)
  95.     Config.Count = CfgNo
  96.     address(thorport)
  97.     CLOSEPROGRESS req ProgWin
  98.     end
  99. ;;;
  100. /* ;;;Main loop */
  101. address(thorport)
  102. do until pos(upper(Choice),'SAVEUSE') > 0
  103.     Choice = ShowMain()
  104.     select
  105.         when Choice = Menu.Line.1 then call AddCfg
  106.         when Choice = Menu.Line.2 then call EditCfg(0)
  107.         when Choice = Menu.Line.3 then call DeleteCfg
  108.         when Choice = 'Use'  then call UseCfg
  109.         when Choice = 'Save' then call SaveCfg
  110.         when Choice = 'Help' then call ShowHelp
  111.         otherwise leave
  112.         end
  113.     end
  114. ;;;
  115. /* ;;;Exit, after checking if config should be saved */
  116. if Changed = 1 then REQUESTNOTIFY '"You have changed the configuration.\nDo you want to save it before exiting?"' '"_Save|_Use|E_xit"'
  117. if RC = 30 then call ExitMsg(THOR.LASTERROR)
  118. select
  119.     when result = 1 then call SaveCfg
  120.     when result = 2 then call UseCfg
  121.     otherwise nop
  122.     end
  123.  
  124. exit
  125. ;;;
  126.  
  127. /* ========== Procedures ========== */
  128.  
  129. /* ;;;Exit with error message */
  130. ExitMsg:
  131.     address(thorport)
  132.     parse arg errmsg
  133.     REQUESTNOTIFY '"'errmsg'"' '"Abort"'
  134.     exit
  135. ;;;
  136. /* ;;;Show main menu */
  137. ShowMain:
  138.     address(thorport)
  139.     do until result ~= ' '
  140.         drop PICKED.
  141.         REQUESTLIST instem Menu.LINE TITLE '"CfgAutoReply.thor    "' SIZEGADGET
  142.         end
  143.     if RC = 5 then return 'QUIT'
  144.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  145.     return result
  146. ;;;
  147. /* ;;;Edit a configuration */
  148. EditCfg:
  149.     arg CfgNo
  150.     OldChanged = Changed
  151.  
  152.     /* Select config to edit if not specified */
  153.     if CfgNo = 0 then do
  154.         if Config.Count = 0 then do                         /* Return if no configs defined */
  155.             REQUESTNOTIFY '"You need to create a configuration\nitem before you can edit it ;-)"' '" OK "'
  156.             return 5
  157.             end
  158.  
  159.         REQUESTLIST instem Config title '"Choose config to edit"'
  160.         if RC = 5 then return 5
  161.         if RC > 0 then call ExitMsg(THOR.LASTERROR)
  162.         EditCfg = result
  163.         do i = 1 to Config.Count
  164.             if Config.i = EditCfg then leave
  165.             end
  166.         CfgNo = i
  167.         end
  168.  
  169.     /* Copy values to temporary variable for editing */
  170.     drop EditTemp.
  171.     EditTemp.Config = Config.CfgNo
  172.     do i = 2 to Fields.Name.Count
  173.         if symbol('Config.CfgNo.'Fields.Name.i) = 'VAR' then interpret 'EditTemp.'Fields.Name.i' = Config.CfgNo.'Fields.Name.i
  174.         else interpret 'EditTemp.'Fields.Name.i' = ""'
  175.         end
  176.  
  177.     /* Display edit menu */
  178.     do forever
  179.         /* Set up menu */
  180.         drop EditMenu.
  181.         EditMenu.Count = Fields.Name.Count + 4
  182.         do i = 1 to Fields.Name.Count
  183.             interpret 'EditMenu.i = left(Fields.Menu.i||":                         ",22)||EditTemp.'Fields.Name.i
  184.             end
  185.  
  186.         interpret 'EditMenu.'EditMenu.Count-3' = ""'
  187.         interpret 'EditMenu.'EditMenu.Count-2' = "Accept"'
  188.         interpret 'EditMenu.'EditMenu.Count-1' = ""'
  189.         interpret 'EditMenu.'EditMenu.Count' = "Help"'
  190.  
  191.         /* Select field to edit */
  192.         REQUESTLIST instem EditMenu title '"Choose a field to edit"'
  193.         if RC = 5 then do
  194.             Changed = OldChanged
  195.             return 5
  196.             end
  197.         if RC > 0 then call ExitMsg(THOR.LASTERROR)
  198.         EditItem = result
  199.  
  200.         select
  201.             when EditItem = ''       then iterate
  202.             when EditItem = 'Help'   then do
  203.                 call ShowHelp
  204.                 iterate
  205.                 end
  206.             when EditItem = 'Accept' then leave
  207.             otherwise do
  208.                 do i = 1 to EditMenu.Count
  209.                     if EditMenu.i = EditItem then leave
  210.                     end
  211.                 ItemNo = i
  212.                 end
  213.             end
  214.  
  215.         parse var EditItem EditTitle ':' EditValue
  216.         EditValue = strip(EditValue,L)
  217.  
  218.         /* Call edit procedure for that field */
  219.         do i = 1 to Fields.Name.Count
  220.             if EditTitle = Fields.Menu.i then do
  221.                 interpret 'call Edit'||Fields.Name.i
  222.                 interpret 'EditTemp.'||Fields.Name.i '= EditValue'
  223.                 Changed = 1
  224.                 leave
  225.                 end
  226.             end
  227.  
  228.         end
  229.  
  230.     /* Copy values back from temporary variable */
  231.     Config.CfgNo = EditTemp.Config
  232.     do i = 2 to Fields.Name.Count
  233.         interpret 'tmp = EditTemp.'Fields.Name.i
  234.         if tmp > '' then interpret 'Config.CfgNo.'Fields.Name.i' = tmp'
  235.         else interpret 'drop Config.CfgNo.'Fields.Name.i
  236.         end
  237.  
  238.     return 0
  239. ;;;
  240. /* ;;;Add a configuration */
  241. AddCfg:
  242.     EditValue = ''
  243.     call EditCONFIG                                         /* Get config name */
  244.     if EditValue = '' then return
  245.     do i = 1 to Config.Count                                /* Check this name is not already used */
  246.         if upper(EditValue) = upper(Config.i) then do
  247.             'REQUESTNOTIFY "There is already a configuration item called 'Config.i'." bt " OK "'
  248.             return
  249.             end
  250.         end
  251.     CfgNo = Config.Count+1
  252.     Config.CfgNo = EditValue
  253.     EditValue = ''
  254.     call EditSYSTEM                                         /* Get system */
  255.     if EditValue = '' then return
  256.     Config.CfgNo.SYSTEM = EditValue
  257.     EditValue = ''
  258.     call EditCONFERENCE                                     /* Conference */
  259.     if EditValue = '' then return
  260.     Config.CfgNo.CONFERENCE = EditValue
  261.     EditValue = ''
  262.     call EditTEXTFILE                                       /* Reply text */
  263.     if EditValue = '' then return
  264.     Config.CfgNo.TEXTFILE = EditValue
  265.     if EditCfg(CfgNo) = 0 then do                           /* Edit configuration */
  266.         Changed = 1
  267.         Config.Count = CfgNo
  268.         end
  269.     return
  270. ;;;
  271. /* ;;;Alter individual configuration items */
  272. EditCONFIG:
  273.     REQUESTSTRING title '"CfgAutoReply"' body '"Name of configuration item   "' BT '" _OK |Cancel"' ID '"'EditValue'"'
  274.     if RC = 5 then return
  275.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  276.     EditValue = result
  277.     return
  278.  
  279. EditSYSTEM:
  280.     address(bbsread)
  281.     drop BBSS.
  282.     GETBBSLIST stem BBSS
  283.     if RC = 30 then call ExitMsg(BBSREAD.LASTERROR)
  284.     address(thorport)
  285.     REQUESTLIST instem BBSS title '"Name of system containing incoming messages   "'
  286.  
  287.     if RC = 5 then return
  288.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  289.     EditValue = result
  290.     return
  291.  
  292. EditCONFERENCE:
  293.     address(bbsread)
  294.     drop CONFS.
  295.     GETCONFLIST bbsname '"'Config.CfgNo.System'"' stem CONFS
  296.     if RC = 30 then call ExitMsg(BBSREAD.LASTERROR)
  297.     address(thorport)
  298.     REQUESTLIST instem CONFS title '"Name of conference containing incoming messages   "'
  299.     if RC = 5 then return
  300.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  301.     EditValue = result
  302.     return
  303.  
  304. EditTEXTFILE:
  305.     call SplitPath()
  306.     REQUESTFILE title '"Please select the reply text file"' ID '"'path'"' IF '"'file'"' FULLPATH
  307.     if RC = 5 then return
  308.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  309.     EditValue = result
  310.     return
  311.  
  312. EditSUBJECT:
  313.     REQUESTSTRING title '"CfgAutoReply"' body '"Subject line for reply\nUse %%s to include original subject\nDefault is Re: %s"' BT '" _OK |Cancel"' ID '"'EditValue'"'
  314.     if RC = 5 then return
  315.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  316.     EditValue = result
  317.     return
  318.  
  319. EditQUOTEMSG:
  320.     REQUESTNOTIFY '"Where would you like the quote the incoming mail,\n above your reply, below it, or not at all?"' '"_Above|_Below|_NoQuote"'
  321.     if RC = 5 then return
  322.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  323.     select
  324.         when result = 1 then EditValue = 'Above'
  325.         when result = 2 then EditValue = 'Below'
  326.         otherwise EditValue = ''
  327.         end
  328.     return
  329.  
  330. EditQUOTESTR:
  331.     REQUESTSTRING title '"CfgAutoReply"' body '"The string or character prepended to quoted lines"' BT '" _OK |Cancel"' ID '"'EditValue'"'
  332.     if RC = 5 then return
  333.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  334.     EditValue = result
  335.     return
  336.  
  337. EditSIGFILE:
  338.     call SplitPath()
  339.     REQUESTFILE title '"Please select signature file"' ID '"'path'"' IF '"'file'"' FULLPATH
  340.     if RC = 5 then return
  341.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  342.     EditValue = result
  343.     return
  344.  
  345. EditHEADFILE:
  346.     call SplitPath()
  347.     REQUESTFILE title '"Please select a header file"' ID '"'path'"' IF '"'file'"' FULLPATH
  348.     if RC = 5 then return
  349.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  350.     EditValue = result
  351.     return
  352.  
  353. EditFOOTFILE:
  354.     call SplitPath()
  355.     REQUESTFILE title '"Please select a footer file"' ID '"'path'"' IF '"'file'"' FULLPATH
  356.     if RC = 5 then return
  357.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  358.     EditValue = result
  359.     return
  360. ;;;
  361. /* ;;;Split the current EditValue into path and file */
  362. SplitPath:
  363.     select
  364.         when lastpos('/',EditValue) > 0 then do
  365.             pathend = lastpos('/',EditValue)
  366.             path = left(EditValue,pathend-1)
  367.             file = substr(EditValue,pathend+1)
  368.             end
  369.         when lastpos(':',EditValue) > 0 then do
  370.             pathend = lastpos(':',EditValue)
  371.             path = left(EditValue,pathend)
  372.             file = substr(EditValue,pathend+1)
  373.             end
  374.         when EditValue = '' then do
  375.             Path = 'RAM:'
  376.             File = ''
  377.             end
  378.         otherwise do
  379.             path = ''
  380.             file = EditValue
  381.             end
  382.         end
  383.     return
  384. ;;;
  385. /* ;;;Delete a configuration */
  386. DeleteCfg:
  387.     /* Select config to delete */
  388.     REQUESTLIST instem Config title '"Choose configuration to delete  "'
  389.     if RC = 5 then return
  390.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  391.     DelCfg = result
  392.     REQUESTNOTIFY '"'DelCfg||':\nAre you sure you want to delete this configuration?"' '"_Delete|_Cancel"'
  393.     if result = 0 then return
  394.     Changed = 1
  395.     do i = 1 to Config.Count
  396.         if Config.i = DelCfg then leave
  397.         end
  398.     CfgNo = i
  399.  
  400.     Config.Count = Config.Count - 1
  401.     if CfgNo <= Config.Count then do i = CfgNo to Config.Count
  402.         x = i+1
  403.         Config.i = Config.x
  404.         do j = 1 to Fields.Name.Count
  405.             if symbol('Config.x.'Fields.Name.j) = 'VAR' then do
  406.                 interpret 'Config.i.'Fields.Name.j '= Config.x.'Fields.Name.j
  407.                 end
  408.             else do
  409.                 interpret 'drop Config.i.'Fields.Name.j
  410.                 end
  411.             end
  412.         end
  413.  
  414.     return
  415. ;;;
  416. /* ;;;Save the config to ENV: */
  417. UseCfg:
  418.     if exists(CfgFile) then address command 'copy >NIL:' CfgFile CfgFile'.bak'
  419.     if ~open(cfg,CfgFile,'w') then call ExitMsg('Failed to open configuration file to save')
  420.  
  421.     /* Open progress window */
  422.     OPENPROGRESS title '"CfgAutoReply.thor"' PT '"Writing configuration"'
  423.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  424.     ProgWin = result
  425.  
  426.     do i = 1 to Config.Count
  427.         call writeln(cfg,'CONFIG       "'Config.i'"')
  428.         do j = 2 to Fields.Name.Count
  429.             if symbol('Config.i.'Fields.Name.j) ~= 'VAR' then iterate
  430.             CfgName = left(Fields.Name.j||'             ',13)
  431.             interpret 'CfgVal = Config.i.'Fields.Name.j
  432.             call writeln(cfg,CfgName'"'CfgVal'"')
  433.             end
  434.             call writeln(cfg,'ENDCONFIG')
  435.             call writeln(cfg,'')
  436.  
  437.         /* Update progress bar */
  438.         UPDATEPROGRESS req ProgWin current i*100%Config.Count
  439.         end
  440.     call close(cfg)
  441.     CLOSEPROGRESS req ProgWin
  442.     Changed = 0
  443.     return
  444. ;;;
  445. /* ;;;Save the config to ENVARC: */
  446. SaveCfg:
  447.     call UseCfg()
  448.     address command 'copy >NIL:' CfgFile'#? TO ENVARC:THOR'
  449.     return
  450. ;;;
  451. /* ;;;Call multiview to display help file */
  452. ShowHelp:
  453.     drop GLOBALCFG.
  454.     GETGLOBALCONFIG stem GLOBALCFG              /* Get global information */
  455.     address command 'MultiView `GetEnv THOR/THORPath`docs/AutoReply.guide PUBSCREEN' GLOBALCFG.PUBSCREENNAME
  456.     return
  457. ;;;
  458.